#include "List.h"
template <class T>

    List<T>::List():_head( new Node<T>(T()))//这里头结点用T泛型的匿名变量来初始化
    {
        _head->_next=_head;
        _head->_prev=_head;
    }
template <class T>
    List<T>::List(const List<T> &L1):_head(new Node<T>(T()))
   {
        _head->_next=_head;
        _head->_prev=_head;
        Node<T> *cur=L1._head->_next;
        while(cur!=L1._head)
        {
            PushBack(cur->_data);
            cur=cur->_next;
        }
   }
template <class T>
    List<T> & List<T>::operator=(const List<T> &L1)
   {//TODO
       List<T> L2(L1);//先用L1拷贝构造L2,再将L2和this交换
       swap(this->_head,L2._head);
        //函数调用结束后会自己调析构函数来释放L2即释放旧的this指向的内容
   }
template <class T>
    List<T>::~List()
    {
        Node<T> *cur=_head->_next;
        while(cur!=_head)
        {
            PopBack();
            cur=cur->_next;
        }
        delete _head;
    }
template <class T>
    Node<T> * List<T>::Insert( Node<T> * pos, const T& x )
    {
        assert(pos);
        Node<T> * new_node=new Node<T>(x);
        Node<T> * prev=pos->_prev;

        prev->_next=new_node;
        new_node->_prev=prev;

        new_node->_next=pos;
        pos->_prev=new_node;
        return new_node;
    }
template <class T>
    void List<T>::Erase(Node<T> *pos)
    {
        assert(pos);
        assert(pos!=_head);
        Node<T> * prev=pos->_prev;
        Node<T> * next=pos->_next;

        prev->_next=next;
        next->_prev=prev;
        delete pos;
    }
template <class T>
    Node<T> * List<T>::PushBack( const T &x )
    {
        return Insert(_head,x);
    }
template <class T>
    void List<T>::PushFront(const T &x)
    {
        Insert(_head->_next,x);
    }
template <class T>
    void List<T>::PopBack()
    {
        Erase(_head->_prev);
    }
template <class T>
    void List<T>::PopFront()
    {
        Erase(_head->_next);
    }
template <class T>
    size_t List<T>::Size() const
    {
        size_t count=0;
        Node<T> *cur=_head->_next;
        while(cur!=_head)
        {
            count++;
            cur=cur->_next;
        }
        return count;
    }
template <class T>
    bool List<T>::Empty()
    {
        return _head->_next==_head;
    }
template <class T>
    const T & List<T>::Back()const
    {
        return _head->_prev->_data;
    }
template <class T>
    const T & List<T>::Front()const
    {
        return _head->_next->_data;
    }
template <class T>
    void List<T>::Clear()//清理掉链表中的数据
    {
        Node<T> *cur=_head->_next;
        while(cur!=_head)
        {
            PopBack();
            cur=cur->_next;
        }
        _head->_next=_head;
        _head->_prev=_head;
    }
template <class T>
void List<T>::Display()
{
    Node<T> *cur=_head->_next;
    while(cur!=_head)
    {
        cout<<cur->_data<<" ";
        cur=cur->_next;
    }
    cout<<endl;
}
